/**
 *  Concrete PublicRESTAdaptor implementation 
 *
 *  @author Scott Monahan
 *  @version $Id$
 *  
 *  File last changed on $Date:$ by $Author:$ in $Rev:$
 *
 */

package info637.pprc.adaptor;

import info637.pprc.ErrorCodes;
import info637.pprc.model.Property;
import info637.pprc.services.IPropertyService;
import info637.pprc.services.IPropertyService.PropertyDetailResponse;
import info637.pprc.services.IPropertyService.PropertyListResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component(value = "publicRESTAdaptor")
public class PublicRESTAdaptor extends AbstractRESTAdaptor
	implements IPublicRESTAdaptor {
	
	@Autowired
	private IPropertyService propertyService = null;
	
	@Value("${version}")
	private String version = null;

	@Override
	public java.util.Map<String, Object> getPropertyListings(String requestId, 
		String address, String city, String state, String zip, String monthlyRent) {

		java.util.Map<String, Object> out = new java.util.HashMap<String, Object>();
		
		out.put(APIConstants.requestId, requestId);
		
		List<ErrorCodes> validationErrors = validateBaseRequest(requestId);
		
		if (isStringValid(monthlyRent) && !validateNumeric(monthlyRent)) {
			
			validationErrors.add(ErrorCodes.INVALID_MONTHLY_RENT);
		}
		
		if (validationErrors.size() == 0) {
		
			PropertyListResponse response 
				= this.propertyService.findByPublicFilterData(address, city, state, zip, monthlyRent);
			
			if (response.isError()) {
				
				out.put(APIConstants.errorCodes, this.errorCodesToString(response.getErrorCodes()));
				out.put(APIConstants.status, "1");
				
			} else {
				
				List<Map<String, String>> outProperties = new ArrayList<Map<String, String>>();
				
				for (Property property : response.getPropertyList()) {
					
					java.util.Map<String, String> propertyMap = new java.util.HashMap<String, String>();
					
					propertyMap.put(APIConstants.propertyId, property.getId());
					propertyMap.put(APIConstants.address, property.getAddress());
					propertyMap.put(APIConstants.city, property.getCity());
					propertyMap.put(APIConstants.state, property.getState());
					propertyMap.put(APIConstants.zip, property.getZip());
					propertyMap.put(APIConstants.monthlyRent, property.getMonthlyRent());
					
					outProperties.add(propertyMap);
				}
				
				out.put(APIConstants.properties, outProperties);
				out.put(APIConstants.status, "0");
			}
		
		} else {
			
			out.put(APIConstants.errorCodes, this.errorCodesToString(validationErrors));
			out.put(APIConstants.status, "1");
		}
		
		return out;
	}

	@Override
	public Map<String, String> getPropertyDetail(String requestId, String propertyId) {

		java.util.Map<String, String> out = new java.util.HashMap<String, String>();
		
		out.put(APIConstants.requestId, requestId);
		
		List<ErrorCodes> validationErrors = validateBaseRequest(requestId);
		
		if (!isStringValid(propertyId)) {
			
			validationErrors.add(ErrorCodes.PROPERTY_ID_REQUIRED);
		}
		
		if (validationErrors.size() == 0) {
			
			PropertyDetailResponse response = this.propertyService.getPropertyDetail(propertyId);
			
			if (response.isError()) {
				
				out.put(APIConstants.errorCodes, this.errorCodesToString(response.getErrorCodes()));
				out.put(APIConstants.status, "1");
				
			} else {
				
				out.put(APIConstants.status, "0");
				
				out.put(APIConstants.requestId, requestId);
				out.put(APIConstants.address, response.getProperty().getAddress());
				out.put(APIConstants.city, response.getProperty().getCity());
				out.put(APIConstants.state, response.getProperty().getState());
				out.put(APIConstants.zip, response.getProperty().getZip());
				out.put(APIConstants.monthlyRent, response.getProperty().getMonthlyRent());
				out.put(APIConstants.securityDeposit, response.getProperty().getSecurityDeposit());
				out.put(APIConstants.numOfBeds, response.getProperty().getNumOfBeds());
				out.put(APIConstants.numOfBaths, response.getProperty().getNumOfBaths());
				out.put(APIConstants.sqft, response.getProperty().getSqft());
				out.put(APIConstants.appliances, response.getProperty().getAppliances());
				out.put(APIConstants.description, response.getProperty().getDescription());
				out.put(APIConstants.contactInfo, response.getProperty().getContactInfo());
			}
		
		} else {
			
			out.put(APIConstants.errorCodes, this.errorCodesToString(validationErrors));
			out.put(APIConstants.status, "1");
		}
	
		return out;
	}

	@Override
	public Map<String, String> ping() {
		
		Map<String, String> out = new HashMap<String, String>();
		
		out.put(APIConstants.status, "0");
		out.put(APIConstants.version, this.version);
		
		return out;
	}
}